package com.zhijiang.hospital.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhijiang.hospital.entity.dto.OutpatientDepartmentDto;
import com.zhijiang.hospital.entity.dto.OutpatientsRunDto;
import com.zhijiang.hospital.mapper.OutpatientDepartmentMapper;
import com.zhijiang.hospital.mapper.OutpatientsRunMapper;
import com.zhijiang.hospital.service.OutpatientDepartmentService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.NumberUtils;
import com.zhijiang.util.PageList;
import com.zhijiang.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital_data
 * @description
 * @date 2022-10-26 10:23:57
 */
@Service
public class OutpatientDepartmentImpl implements OutpatientDepartmentService {
    @Autowired
    private OutpatientDepartmentMapper outpatientDepartmentMapper;

    @Override
    public List<OutpatientDepartmentDto> findAll() {
        return outpatientDepartmentMapper.findAll();
    }


    @Resource
    OutpatientsRunMapper outpatientsRunMapper;

    @Override
    public Map<String, Object> getOutpatientsRunBooking(BaseQuery baseQuery) {
        List<OutpatientsRunDto> outpatientsRunDtos = outpatientsRunMapper.getOutpatientsRun(baseQuery.getStartTime(), baseQuery.getEndTime());
        List<Integer> collect = outpatientsRunDtos.stream().map(OutpatientsRunDto::getBookingNumber).collect(Collectors.toList());
        Integer aLong = collect.stream().reduce(Integer::sum).orElse(0);
        List<Integer> collect1 = outpatientsRunDtos.stream().map(OutpatientsRunDto::getDiagnosisNumber).collect(Collectors.toList());
        Integer aLong1 = collect1.stream().reduce(Integer::sum).orElse(0);
        OutpatientsRunDto outpatientsRunDto = new OutpatientsRunDto();
        outpatientsRunDto.setBookingNumber(aLong);
        outpatientsRunDto.setDiagnosisNumber(aLong1);
        outpatientsRunDto.setDeptName("合计");
        List<Object> list1 = new ArrayList<>();
        list1.add(outpatientsRunDto);
        Map<String, Object> map = new HashMap<>();
        map.put("heji", list1);
        return map;
    }


    @Override
    public List<OutpatientsRunDto> getYearData() {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        String year = String.valueOf(DateUtil.year(dateTime));
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        String startTime = Convert.toStr(dateTime);
        String endTime = Convert.toStr(dateTime1);
        List<OutpatientsRunDto> outpatientsRun = outpatientsRunMapper.getOutpatientsRun(startTime, endTime);
        return outpatientsRun;
    }

    @Override
    public List<OutpatientsRunDto> getLastYearData() {
        Date nextYearPreDay = TimeUtil.getNextYearPreDay();
        DateTime dateTime3 = DateUtil.beginOfYear(nextYearPreDay);
        String lastYear = String.valueOf(DateUtil.year(dateTime3));
        DateTime dateTime4 = DateUtil.endOfYear(nextYearPreDay);
        String startTime = TimeUtil.format(dateTime3);
        String endTime = TimeUtil.format(dateTime4);
        List<OutpatientsRunDto> outpatientsRun = outpatientsRunMapper.getOutpatientsRun(startTime, endTime);
        return outpatientsRun;
    }

    @Override
    public PageList getOutpatientsRunBookingByPageList(BaseQuery query) {
        PageHelper.startPage(query.getPage(), query.getPageSize());
        List<OutpatientsRunDto> outpatientsRunDtos = outpatientsRunMapper.getOutpatientsRun(query.getStartTime(), query.getEndTime());
        PageInfo<OutpatientsRunDto> pageInfo = new PageInfo<>(outpatientsRunDtos);
        Integer pages = NumberUtils.totalPages(pageInfo.getTotal(), query.getPageSize());
        PageList<OutpatientsRunDto> outpatientsRunDtoPageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList(), query.getPage(), query.getPageSize(), pages);
        return outpatientsRunDtoPageList;
    }


}
